<html>
<head><meta charset="utf-8"><title>Strange Error with Enum and for loop · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html">Strange Error with Enum and for loop</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218653306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218653306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Krauthan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218653306">(Dec 03 2020 at 04:41)</a>:</h4>
<p>I just had the following code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">map_yaml</span><span class="p">.</span><span class="n">as_hash</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">match</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">Yaml</span>::<span class="n">Real</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">Yaml</span>::<span class="nb">String</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">clone</span><span class="p">()),</span><span class="w"></span>
<span class="w">                </span><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">return</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"true"</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"false"</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">};</span><span class="w"></span>
<span class="w">                </span><span class="p">},</span><span class="w"></span>
<span class="w">                </span><span class="n">Yaml</span>::<span class="n">Integer</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">to_string</span><span class="p">()),</span><span class="w"></span>
<span class="w">                </span><span class="n">Yaml</span>::<span class="n">Null</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"null"</span><span class="p">)),</span><span class="w"></span>
<span class="w">                </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">panic</span><span class="o">!</span><span class="p">(</span><span class="s">"Unknown key type"</span><span class="p">),</span><span class="w"></span>
<span class="w">            </span><span class="p">};</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But when it run into the <code>Yaml::Boolean</code> case the for loop just ended (no error message or anything). After switching it out to </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="kc">true</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"true"</span><span class="p">)),</span><span class="w"></span>
<span class="w">                    </span><span class="kc">false</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"false"</span><span class="p">)),</span><span class="w"></span>
<span class="w">                </span><span class="p">},</span><span class="w"></span>
</code></pre></div>
<p>everything runs as expected. Can anyone tell me why the first code just ended the for loop?</p>



<a name="218654117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218654117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nyms <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218654117">(Dec 03 2020 at 05:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369173">Florian Krauthan</span> <a href="#narrow/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop/near/218653306">said</a>:</p>
<p>I just had the following code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">                </span><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">return</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"true"</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"false"</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>In the above snippet, you are <code>return</code>ing the <code>()</code> value. That short-circuits the for loop, and returns.</p>



<a name="218654546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218654546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Ellis O&#x27;Riley Jr. <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218654546">(Dec 03 2020 at 05:11)</a>:</h4>
<p><span class="user-mention" data-user-id="354190">@nyms</span>  Just learned about () this week.  It’s actually called a unit</p>



<a name="218655137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218655137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Krauthan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218655137">(Dec 03 2020 at 05:27)</a>:</h4>
<p>Oh interesting. But how can I return the <code>()</code> value if the match expects an <code>Option&lt;String&gt;</code>? It might have been IDEA's fault since I had it as</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">                </span><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="k">return</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"true"</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="k">return</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"false"</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>and it suggested to refactor it to the other style?</p>



<a name="218657582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218657582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Ellis O&#x27;Riley Jr. <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218657582">(Dec 03 2020 at 06:24)</a>:</h4>
<p><span class="user-mention" data-user-id="369173">@Florian Krauthan</span> Can you explain to me the purpose of the return statement?  Thanks.</p>



<a name="218657756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218657756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Krauthan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218657756">(Dec 03 2020 at 06:29)</a>:</h4>
<p><span class="user-mention" data-user-id="344328">@Charles Ellis O'Riley Jr.</span> I am still pretty new to Rust. In theory I don't need the return at all (as the important part os the <code>config.insert</code> but the rust compiler didn't let me not return a value as all other arms are returning an Option (since that is what hashMap.insert is returning).</p>



<a name="218658042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218658042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Ellis O&#x27;Riley Jr. <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218658042">(Dec 03 2020 at 06:36)</a>:</h4>
<p>I'm pretty new to Rust as well.  Since you didn't have a semicolon at the end of the expression(s), I just didn't think the return was needed.  So, you're returning either "true" or "false".  I'm not caught up on the Option&lt;String&gt;</p>



<a name="218692329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218692329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Snoyman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218692329">(Dec 03 2020 at 13:29)</a>:</h4>
<p>All of the arms of a match expression, or all of the branches of an if/then/else, need to line up. If you have some arms which are evaluating to a value of a different type (like Option&lt;String&gt;) but you're not using them, my recommendation would be to wrap those arms in braces and then stick a semicolon at the end of the expression to explicitly discard that value. Then every arm will evaluate to the same type, unit <code>()</code></p>



<a name="218695505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218695505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218695505">(Dec 03 2020 at 13:58)</a>:</h4>
<p><span class="user-mention" data-user-id="368558">@Michael Snoyman</span> if you drop <em>both</em> the <code>return</code> keyword <em>and</em> the <code>;</code> at the end of both <code>if … else </code> branches, then your code should be equivalent to the second one with the <code>match</code></p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code>                Yaml::Boolean(b) =&gt; {
<span class="gd">-                   return if *b == true {</span>
<span class="gi">+                   if *b {</span>
<span class="gd">-                       config.insert(String::from(key.as_str().unwrap()), String::from("true"));</span>
<span class="gi">+                       config.insert(String::from(key.as_str().unwrap()), String::from("true"))</span>
                    } else {
<span class="gd">-                       config.insert(String::from(key.as_str().unwrap()), String::from("false"));</span>
<span class="gi">+                       config.insert(String::from(key.as_str().unwrap()), String::from("false"))</span>
<span class="gd">-                   };</span>
<span class="gi">+                   }</span>
                }
</code></pre></div>



<a name="218696223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218696223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218696223">(Dec 03 2020 at 14:03)</a>:</h4>
<p>Also note that you can bundle the <code>bool</code> check inside the <code>match</code> arms:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="o">&amp;</span><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"true"</span><span class="p">)),</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="kc">false</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">"false"</span><span class="p">)),</span><span class="w"></span>
</code></pre></div>
<p>or use the <code>if … else …</code> as a ternary:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="o">&amp;</span><span class="n">Yaml</span>::<span class="n">Boolean</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">key</span><span class="p">.</span><span class="n">as_str</span><span class="p">().</span><span class="n">unwrap</span><span class="p">()),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">"true"</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">"false"</span><span class="w"> </span><span class="p">})),</span><span class="w"></span>
</code></pre></div>



<a name="218731627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Strange%20Error%20with%20Enum%20and%20for%20loop/near/218731627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Krauthan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Strange.20Error.20with.20Enum.20and.20for.20loop.html#218731627">(Dec 03 2020 at 18:02)</a>:</h4>
<p>Oh cool. The more I learn about Rust the more I like a lot of the features it offers.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>